<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="">
              <a-input placeholder="关键词搜索" v-model="queryParam.queryName"></a-input>
            </a-form-item>
          </a-col>

          <a-col :xl="7" :lg="7" :md="8" :sm="24">
            <a-form-item label="创建时间">
              <j-date placeholder="请选择创建时间" v-model="queryParam.queryCode"></j-date>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery">查询</a-button>
              <a-button type="primary" @click="searchReset" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>

        <div style="width: 100%;clear: both">
          <a-button v-show="this.tabs==1" :href="exportFabric" style="margin-left: 0px" type="primary"
                    icon="export">导出里布</a-button>
          <a-button v-show="this.tabs==2" :href="exportFabric2" style="margin-left: 8px" type="primary"
                    icon="export">导出底领呢</a-button>
          <a-button v-show="this.tabs==3" :href="exportFabric3" style="margin-left: 8px" type="primary"
                    icon="export">导出纽扣</a-button>

          <a-upload v-show="this.tabs==1" style="margin-left: 8px" name="file" :showUploadList="false"
                    :multiple="false"
                    :customRequest="myupload1"
                    :action="importExcelUrl"
                    @change="handleImportExcel">
            <a-button type="primary" icon="import"><upload-outlined/>导入里布</a-button>
          </a-upload>

          <a-upload v-show="this.tabs==2" style="margin-left: 8px" name="file" :showUploadList="false"
                    :multiple="false"
                    :customRequest="myupload2"
                    :action="importExcelUrl"
                    @change="handleImportExcel">
            <a-button type="primary" icon="import"><upload-outlined/>导入底领呢</a-button>
          </a-upload>

          <a-upload v-show="this.tabs==3" style="margin-left: 8px" name="file" :showUploadList="false"
                    :multiple="false"
                    :customRequest="myupload3"
                    :action="importExcelUrl"
                    @change="handleImportExcel">
            <a-button type="primary" icon="import"><upload-outlined/>导入纽扣</a-button>
          </a-upload>

          <a-button type="primary" @click="add_category(0)" icon="plus"
                    style="margin-left: 8px">
            新增
          </a-button>


          <a-button :loading="loading_batch" type="primary" @click="delete_batch_fuliao()" icon="el-icon-delete"
                    style="margin-left: 8px" v-show="this.select_ids.length>0">
            批量删除
          </a-button>

          <a-button :loading="loading_clean" type="danger" style="margin-left: 8px;float: right" @click="open_tishiKuang" ghost>
            一键清空
          </a-button>

        </div>
      </a-form>
    </div>

    <div style="width: 100%;clear: both;margin-top: 10px" v-show="this.select_ids.length>0">
      <span style="color: #9a9797;font-size: 13px">你已选中了</span>
      <span style="color: #2d2d2d;font-size: 16px;font-weight: bolder;margin-left: 3px;margin-right: 3px">
        {{this.select_ids.length}}</span>个{{this.isbuttons}}
    </div>


    <div style="background: #f2f2f2;height: 10px;width: auto;margin-top: 10px;margin-bottom: 30px;"/>
    <div>
      <div style="border: none;background-color: #ffffff;padding-bottom: 28px;">
        <!--        <span style="font-size: 16px;float: left;color:#000000;font-weight: bold">品类列表</span>-->
        <a-tabs @change="bubu" style="margin-top: -20px">
          <a-tab-pane tab="里布列表" key="1"></a-tab-pane>
          <a-tab-pane tab="底领呢列表" key="2"></a-tab-pane>
          <a-tab-pane tab="纽扣列表" key="3"></a-tab-pane>
        </a-tabs>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange" v-show="this.tabs==1" style="margin-top: -20px;">

        <template slot="avatarslot" slot-scope="text, record, index">
          <div class="anty-img-wrap">
            <a-avatar shape="square" :src="getAvatarView(record.libuImg)" icon="img"/>
          </div>
        </template>

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
         <div align="center" style="align-content: center;margin-left: 30px;">
          <a-menu-item style="float: left;margin-left: 10px;">
             <a-popconfirm title="确定移除该里布吗?" @confirm="() => delete_location(record.id)">
                  <a style="font-size: 14px;color: #d29328;">删除</a>
                </a-popconfirm>
          </a-menu-item>
          </div>
        </span>
      </a-table>


      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :columns="columns2"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange" v-show="this.tabs==2" style="margin-top: -20px;">

        <template slot="avatarslot" slot-scope="text, record, index">
          <div class="anty-img-wrap">
            <a-avatar shape="square" :src="getAvatarView(record.img)" icon="img"/>
          </div>
        </template>

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <div align="center" style="align-content: center;margin-left: 30px;">
          <a-menu-item style="float: left;margin-left: 10px;">
             <a-popconfirm title="确定移除该底领呢吗?" @confirm="() => delete_location(record.id)">
                  <a style="font-size: 14px;color: #d29328;">删除</a>
                </a-popconfirm>
          </a-menu-item>
          </div>
        </span>
      </a-table>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :columns="columns3"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange" v-show="this.tabs==3" style="margin-top: -20px;">

        <template slot="avatarslot" slot-scope="text, record, index">
          <div class="anty-img-wrap">
            <a-avatar shape="square" :src="getAvatarView(record.buttonsImg)" icon="img"/>
          </div>
        </template>

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </a-button>
        </template>
        <span slot="action" slot-scope="text, record">
          <div align="center" style="align-content: center;margin-left: 30px;">
          <a-menu-item style="float: left;margin-left: 10px;">
             <a-popconfirm title="确定移除该纽扣吗?" @confirm="() => delete_location(record.id)">
                  <a style="font-size: 14px;color: #d29328;">删除</a>
                </a-popconfirm>
          </a-menu-item>
          </div>
        </span>
      </a-table>
    </div>

    <AddLocationModule ref="modalForm1" @ok="modalFormOk"></AddLocationModule>
    <AddColorModule ref="modalForm2" @ok="modalFormOk"></AddColorModule>
    <AddXiuziModle ref="modalForm3" @ok="modalFormOk"></AddXiuziModle>
  </a-card>
</template>

<script>

import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import {putAction, httpAction, getFileAccessHttpUrl, postAction} from '@/api/manage';
import JDate from '@/components/jeecg/JDate.vue'
import '@/assets/less/TableExpand.less'
import AddLocationModule from './modules/AddLocationModule'
import AddColorModule from './modules/AddColorModule'
import AddXiuziModle from './modules/AddXiuziModle'

export default {
  name: "categoryinfo",
  mixins: [JeecgListMixin],
  components: {
    JDate,
    AddLocationModule,
    AddColorModule,
    AddXiuziModle
  },
  data() {
    return {
      description: 'categoryinfo管理页面',
      tabs: 1,//切换框
      /* table选中keys*/
      selectedRowKeys: [],
      /* table选中record*/
      selectionRows: [],
      select_ids:[],//选中的ids
      isbuttons:'里布',
      is_libu:'0',
      loading_batch:false,
      loading_clean:false,
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        // {
        //   title: '里布名称',
        //   align: "center",
        //   dataIndex: 'libuName'
        // },
        {
          title: '里布编号',
          align: "center",
          dataIndex: 'libuCode'
        },
        {
          title: '缩略图片',
          align: "center",
          width: 150,
          dataIndex: 'libuImg',
          scopedSlots: {customRender: "avatarslot"}
        },
        {
          title: '色型',
          align: "center",
          dataIndex: 'colorType',
          customRender(index) {
            if (index == '0') {
              return "单色"
            } else if (index == '1') {
              return "双色"
            } else {
              return "三色"
            }
          }

        },
        {
          title: '里布类型',
          align: "center",
          dataIndex: 'libuType',
          customRender(index) {
            if (index == '0') {
              return "经典里布"
            } else if (index == '1') {
              return "高级里布"
            } else {
              return "时尚里布"
            }
          }
        },
        {
          title: '销售加价',
          align: "center",
          dataIndex: 'salesPrice'
        },
        {
          title: '成本加价',
          align: "center",
          dataIndex: 'costPrice'
        },
        {
          title: '创建时间',
          align: "center",
          dataIndex: 'createTime',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          scopedSlots: {customRender: 'action'},
        }
      ],

      columns2: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        // {
        //   title: '底领呢名称',
        //   align: "center",
        //   dataIndex: 'dilinniName'
        // },
        {
          title: '底领呢编号',
          align: "center",
          dataIndex: 'dilinniNumber'
        },
        {
          title: '缩略图片',
          align: "center",
          width: 150,
          dataIndex: 'buttonsImg',
          scopedSlots: {customRender: "avatarslot"}
        },
        {
          title: '销售加价',
          align: "center",
          dataIndex: 'salesPrice'
        },
        {
          title: '成本加价',
          align: "center",
          dataIndex: 'costPrice'
        },
        {
          title: '创建时间',
          align: "center",
          dataIndex: 'createTime',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          scopedSlots: {customRender: 'action'},
        }
      ],

      columns3: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: '纽扣名称',
          align: "center",
          dataIndex: 'buttonsName'
        },
        {
          title: '纽扣编号',
          align: "center",
          dataIndex: 'buttonsCode'
        },
        {
          title: '缩略图片',
          align: "center",
          width: 150,
          dataIndex: 'buttonsImg',
          scopedSlots: {customRender: "avatarslot"}
        },
        {
          title: '纽扣类别',
          align: "center",
          dataIndex: 'categoryName'
        },
        {
          title: '销售加价',
          align: "center",
          dataIndex: 'salesPrice'
        },
        {
          title: '成本加价',
          align: "center",
          dataIndex: 'costPrice'
        },
        {
          title: '创建时间',
          align: "center",
          dataIndex: 'createTime',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          scopedSlots: {customRender: 'action'},
        }
      ],
      url: {
        list: "/admin/testplate/categoryinfo_material_libu?category_id=" + this.$route.query.category_id,
        delete_xiuzi_location: "/admin/testplate/delete_plate_material",
        delete_batch_fuliao: "/admin/testplate/delete_batch_material",//批量删除
        yijian_clean: "/admin/testplate/real_delete_material",//一键清空
        exportlibu: "help/exportlibu", // 导出
        exportdi: "help/exportdi", // 导出
        exportbotton: "help/exportbotton", // 导出

        importlibu: "importfu/importlibu", // 导入
        importdi: "importfu/importdi", // 导入
        importbutton: "importfu/importbutton", // 导入
      }
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importlibu}`
    },
    importExcelUrl2: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importdi}`
    },
    importExcelUrl3: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importbutton}`
    },
    exportFabric: function () {
      return `${window._CONFIG['domianURL']}/${this.url.exportlibu}?categoryId=${this.$route.query.category_id}`
    },
    exportFabric2: function () {
      return `${window._CONFIG['domianURL']}/${this.url.exportdi}?categoryId=${this.$route.query.category_id}`
    },
    exportFabric3: function () {
      return `${window._CONFIG['domianURL']}/${this.url.exportbotton}?categoryId=${this.$route.query.category_id}`
    }
  },
  methods: {
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
      this.select_ids=[];
      for (var i=0;i<this.selectionRows.length;i++)
      {
        this.select_ids.push(this.selectionRows[i].id);
        // console.log("看一看能不能成功"+this.select_ids[i]);
      }
    },
    open_tishiKuang() {
      this.$confirm('一键清空将会永久删除所有数据且不能再恢复，请谨慎操作，你确认要执行该操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.all_clean_fuliao();
      }).catch(() => {
        // this.$message({
        //   type: 'info',
        //   message: '已取消删除'
        // });
      });
    },

    // 导入尺码excel
    myupload1(data) {
      let fdata = new FormData()
      fdata.append('file', data.file)
      fdata.append('categoryId', this.$route.query.category_id)
      postAction(this.url.importlibu, fdata).then(res => {
        if (res.code == '0') {
          this.$message.success("导入成功")
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },

    // 导入尺码excel
    myupload2(data) {
      let fdata = new FormData()
      fdata.append('file', data.file)
      fdata.append('categoryId', this.$route.query.category_id)
      postAction(this.url.importdi, fdata).then(res => {
        if (res.code == '0') {
          this.$message.success("导入成功")
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },

    // 导入尺码excel
    myupload3(data) {
      let fdata = new FormData()
      fdata.append('file', data.file)
      fdata.append('categoryId', this.$route.query.category_id)
      postAction(this.url.importbutton, fdata).then(res => {
        if (res.code == '0') {
          this.$message.success("导入成功")
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },

    bubu(e) {
      this.total = 0
      this.tabs = e
      if (e == 1) {
        this.isbuttons='里布';
        this.is_libu='0';
        this.select_ids=[];
        this.url.list = '/admin/testplate/categoryinfo_material_libu?category_id=' + this.$route.query.category_id
      } else if (e == 2) {
        this.isbuttons='底领呢';
        this.is_libu='1';
        this.select_ids=[];
        this.url.list = '/admin/testplate/categoryinfo_material_dilingli?category_id=' + this.$route.query.category_id
      } else if (e == 3) {
        this.isbuttons='纽扣';
        this.is_libu='2';
        this.select_ids=[];
        this.url.list = '/admin/testplate/categoryinfo_material_buttons?category_id=' + this.$route.query.category_id
      }
      this.loadData();
    },
    getAvatarView: function (avatar) {
      return getFileAccessHttpUrl(avatar)
    },
    add_category(record) {
      if (this.tabs == 1) {
        this.$refs.modalForm1.edit(record);
        this.$refs.modalForm1.title = "配置里布";
        this.$refs.modalForm1.disableSubmit = true;
      } else if (this.tabs == 2) {
        this.$refs.modalForm2.edit(record);
        this.$refs.modalForm2.title = "配置底领呢";
        this.$refs.modalForm2.disableSubmit = true;
      } else if (this.tabs == 3) {
        this.$refs.modalForm3.edit(record);
        this.$refs.modalForm3.title = "配置纽扣";
        this.$refs.modalForm3.disableSubmit = true;
      }
    },
    delete_location(id) {//删除辅料
      let params = {material_id: id};
      httpAction(this.url.delete_xiuzi_location, params, "put").then((res) => {
        if (res.success) {
          this.$message.success(res.message);
          this.loadData();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    delete_batch_fuliao() {//批量删除辅料
      this.loading_batch=true;
      let params = {ids: this.select_ids};
      httpAction(this.url.delete_batch_fuliao, params, "put").then((res) => {
        this.loading_batch=false;
        if (res.success) {
          this.select_ids=[];
          this.$message.success(res.message);
          this.loadData();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    all_clean_fuliao() {//一键清空辅料
      this.loading_clean=true;
      let params = {type: this.is_libu,category_id:this.$route.query.category_id};
      httpAction(this.url.yijian_clean, params, "put").then((res) => {
        this.loading_clean=false;
        if (res.success) {
          this.select_ids=[];
          this.$message.success(res.message);
          this.loadData();
        } else {
          this.$message.error(res.message);
        }
      });
    }

  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>